const KEY_PREFIX = 'PINIA_STORE_'

export default function(context) {
	const {
		store
	} = context;
	const KEY = KEY_PREFIX + store.$id.toUpperCase();
	//存 
	window.addEventListener('beforeunload', () => {
		localStorage.setItem(KEY, JSON.stringify(store.$state));
	})
	
	setInterval(_ => {
		localStorage.setItem(KEY, JSON.stringify(store.$state));
	}, 1000 * 60)


	//读
	try {
		const localData = localStorage.getItem(KEY)
		if (localData) {
			store.$patch(JSON.parse(localData))
		}
	} catch {
		console.log('本地存储异常')
	}

}